package com.goods.business.service.imp;


import com.goods.business.mapper.*;
import com.goods.business.pojo.Bizsupplier;
import com.goods.business.pojo.InStock;
import com.goods.business.pojo.InStockInfo;
import com.goods.business.pojo.ProductStock;
import com.goods.business.service.InStockService;
import com.goods.common.error.SystemException;
import com.goods.common.vo.business.*;
import com.goods.common.vo.system.UserInfoVO;
import com.goods.system.service.UserService;
import org.apache.ibatis.annotations.Lang;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@ComponentScan(value = "com.goods.business.mappper")
public class InStockServiceImpl implements InStockService {
    @Autowired
    InStockMapper inStockMapper;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    InStockInfoMapper inStockInfoMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ConsumerMapper consumerMapper;
    @Autowired
    ProductStockMapper productStockMapper;
    @Autowired
    UserService userService;

    @Override
    public void inStockAddIntoStock(InStockVO inStockVO) throws SystemException {
        UserInfoVO info = userService.info();
        InStock inStock = new InStock();
        String s = UUID.randomUUID().toString();
        inStock.setInNum(s);
        inStock.setType(inStockVO.getType());
        inStock.setOperator(info.getUsername());
        inStock.setCreateTime(new Date());
        List<Map<String,Object>> products = (List) inStockVO.getProducts();
        inStock.setProductNumber((Integer) products.get(0).get("productNumber"));
        inStock.setSupplierId(inStockVO.getSupplierId());
        inStock.setRemark(inStockVO.getRemark());
        inStock.setStatus(1);
        inStockMapper.insert(inStock);
        InStockInfo inStockInfo = new InStockInfo();
        inStockInfo.setInNum(s);
        inStockInfo.setpNum(productMapper.selectByPrimaryKey((Integer) products.get(0).get("productId")).getPNum());
        inStockInfo.setProductNumber((Integer) products.get(0).get("productNumber"));
        inStockInfo.setCreateTime(new Date());
        inStockInfoMapper.insert(inStockInfo);
    }

    @Override
    public InStockDetailVO selinStockDetaiList(Integer pageNum, Integer id) {
        InStockDetailVO inStockDetailVO = new InStockDetailVO();
        InStock inStock = inStockMapper.selectByPrimaryKey(id.longValue());
        SupplierVO supplierVO = new SupplierVO();
        Bizsupplier bizsupplier = supplierMapper.selectByPrimaryKey(inStock.getSupplierId());
        List<ProductStock> productStocks = productStockMapper.selectAll();
        BeanUtils.copyProperties(bizsupplier, supplierVO);
        inStockDetailVO.setSupplierVO(supplierVO);
        inStockDetailVO.setStatus(inStock.getStatus());
        inStockDetailVO.setType(inStock.getType());
        inStockDetailVO.setOperator(inStock.getOperator());
        List<InStockInfo> inStockInfos = inStockInfoMapper.selectAll();
        List<ProductVO> productVOS = productMapper.selectAll();
        List<InStockItemVO> inStockItemVOS = new ArrayList<>();
        for (InStockInfo inStockInfo : inStockInfos) {
            if (inStockInfo.getInNum().equals(inStock.getInNum())) {
                for (ProductVO productVO : productVOS) {
                    if (inStockInfo.getpNum().equals(productVO.getPNum())) {
                        for (ProductStock productStock : productStocks) {
                            if (productStock.getpNum().equals(productVO.getPNum())) {
                                InStockItemVO inStockItemVO = new InStockItemVO();
                                BeanUtils.copyProperties(productVO, inStockItemVO);
                                inStockItemVO.setCount(productStock.getStock().intValue());
                                inStockItemVOS.add(inStockItemVO);
                            }
                        }
                    }

                }
            }
        }
        inStockDetailVO.setItemVOS(inStockItemVOS);
        return inStockDetailVO;

    }

    @Override
    public List<InStockVO> inStockSelectQuery(Integer pageNum, Integer pageSize, Integer status) {
        List<InStock> inStockVOS = inStockMapper.selectAll();
        List<Bizsupplier> bizsuppliers = supplierMapper.selectAll();
        List<InStockVO> inStockVOS1 = new ArrayList<>();
        if (!StringUtils.isEmpty(status)) {
            for (InStock inStockVO : inStockVOS) {
                for (Bizsupplier bizsupplier : bizsuppliers) {
                    if (status == inStockVO.getStatus() && inStockVO.getSupplierId() == bizsupplier.getId()) {
                        InStockVO inStockVO1 = new InStockVO();
                        BeanUtils.copyProperties(inStockVO, inStockVO1);
                        inStockVO1.setPhone(bizsupplier.getPhone());
                        inStockVO1.setSupplierName(bizsupplier.getName());
                        inStockVOS1.add(inStockVO1);
                    }
                }
            }
            return inStockVOS1;
        }
        return inStockVOS1;
    }

}
